import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })

export class UserAgentInfoService {
  constructor() { }

  getOs() {
    const sUserAgent = navigator.userAgent;
    const isWin = (navigator.platform == 'Win32') || (navigator.platform == 'Win64') || (navigator.platform == 'Windows');
    const isMac = (navigator.platform == 'Mac68K') || (navigator.platform == 'MacPPC') || (navigator.platform == 'Macintosh') || (navigator.platform == 'MacIntel');
    if (isMac) return 'Mac';
    const isUnix = (navigator.platform == 'X11') && !isWin && !isMac;
    if (isUnix) return 'Unix';
    const isLinux = (String(navigator.platform).indexOf('Linux') > -1);
    if (isLinux) return 'Linux';
    if (isWin) {
      const isWin2K = sUserAgent.indexOf('Windows NT 5.0') > -1 || sUserAgent.indexOf('Windows 2000') > -1;
      if (isWin2K) return 'Win2000';
      const isWinXP = sUserAgent.indexOf('Windows NT 5.1') > -1 || sUserAgent.indexOf('Windows XP') > -1;
      if (isWinXP) return 'WinXP';
      const isWin2003 = sUserAgent.indexOf('Windows NT 5.2') > -1 || sUserAgent.indexOf('Windows 2003') > -1;
      if (isWin2003) return 'Win2003';
      const isWinVista = sUserAgent.indexOf('Windows NT 6.0') > -1 || sUserAgent.indexOf('Windows Vista') > -1;
      if (isWinVista) return 'WinVista';
      const isWin7 = sUserAgent.indexOf('Windows NT 6.1') > -1 || sUserAgent.indexOf('Windows 7') > -1;
      if (isWin7) return 'Win7';
      const isWin10 = sUserAgent.indexOf('Windows NT 10.0') > -1 || sUserAgent.indexOf('Windows 10') > -1;
      if (isWin10) return 'Win10';
    }
    return 'None';
  }

  getBrowser() {
    const userAgent = navigator.userAgent;
    const isOpera = userAgent.indexOf('Opera') > -1;
    if (isOpera) {
      return 'Opera';
    }
    if (userAgent.indexOf('Firefox') > -1) {
      return 'Firefox';
    }
    if (userAgent.indexOf('Chrome') > -1) {
      return 'Chrome';
    }
    if (userAgent.indexOf('Safari') > -1) {
      return 'Safari';
    }
    if (userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 && !isOpera) {
      const reIE = new RegExp('MSIE (\\d+\\.\\d+);');
      reIE.test(userAgent);
      const fIEVersion = parseFloat(RegExp.$1);
      if (fIEVersion == 7) {
        return 'IE7';
      } else if (fIEVersion == 8) {
        return 'IE8';
      } else if (fIEVersion == 9) {
        return 'IE9';
      } else if (fIEVersion == 10) {
        return 'IE10';
      } else if (fIEVersion == 11) {
        return 'IE11';
      }
    }
  }
}
